﻿using System;
using System.Windows;                       //STANDARD for WPF App

//STANDARD for WPF App
//STANDARD for WPF App
//STANDARD for WPF App
using System.Windows.Ink;                   //For : InkCanvas
using System.Windows.Input;                 //For : ExecutedRoutedEventArgs

//For : DrawingAttributes
//For : XamlWriter
using System.Windows.Media;                 //For : DrawingGroup

//STANDARD for WPF App
//For : BitmapImage etc etc
//STANDARD for WPF App
//For : Geometric shapes like Line

namespace BitsOfStuff
{
    /// <summary>
    /// Class that deriving stroke for drawing ours shape in ink area
    /// </summary>
    public class CustomStroke : Stroke
    {
        /// <summary>
        /// Constructor of custom ctrokes
        /// </summary>
        /// <param name="pts">Colections of stylus points</param>
        public CustomStroke(StylusPointCollection pts)
            : base(pts)
        {
            this.StylusPoints = pts;
        }

        protected override void DrawCore(DrawingContext drawingContext, DrawingAttributes drawingAttributes)
        {
            if (drawingContext == null)
            {
                throw new ArgumentNullException("drawingContext");
            }
            if (null == drawingAttributes)
            {
                throw new ArgumentNullException("drawingAttributes");
            }
            DrawingAttributes originalDa = drawingAttributes.Clone();
            SolidColorBrush brush2 = new SolidColorBrush(drawingAttributes.Color);
            brush2.Freeze();
            drawingContext.DrawRectangle(brush2, null, new Rect(GetTheLeftTopPoint(), GetTheRightBottomPoint()));
        }

        private Point GetTheLeftTopPoint()
        {
            if (this.StylusPoints == null)
                throw new ArgumentNullException("StylusPoints");
            StylusPoint tmpPoint = new StylusPoint(double.MaxValue, double.MaxValue);
            foreach (StylusPoint point in this.StylusPoints)
            {
                if ((point.X < tmpPoint.X) || (point.Y < tmpPoint.Y))
                    tmpPoint = point;
            }
            return tmpPoint.ToPoint();
        }

        private Point GetTheRightBottomPoint()
        {
            if (this.StylusPoints == null)
                throw new ArgumentNullException("StylusPoints");
            StylusPoint tmpPoint = new StylusPoint(0, 0);
            foreach (StylusPoint point in this.StylusPoints)
            {
                if ((point.X > tmpPoint.X) || (point.Y > tmpPoint.Y))
                    tmpPoint = point;
            }
            return tmpPoint.ToPoint();
        }
    }
}